package com.shixi.shixitraining.shixitraining.ui.tabapply.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.shixi.shixitraining.shixitraining.R;
import com.shixi.shixitraining.shixitraining.base.BaseActivity;
import com.shixi.shixitraining.shixitraining.http.CallbackImple;
import com.shixi.shixitraining.shixitraining.http.HttpUtil;
import com.shixi.shixitraining.shixitraining.ui.tabapply.entity.TrainingDetailBean;
import com.shixi.shixitraining.shixitraining.util.ToastUtil;
import com.shixi.shixitraining.shixitraining.util.UserManager;

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;

import static com.shixi.shixitraining.shixitraining.constant.Constants.APPLYTRAININGDETAIL;

public class TrainingApplyDetailActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.apply_training_title_tv)
    TextView applyTrainingTitleTv;
    @BindView(R.id.apply_training_city_tv)
    TextView applyTrainingCityTv;
    @BindView(R.id.traing_offical_tv)
    TextView traingOfficalTv;
    @BindView(R.id.apply_training_time_tv)
    TextView applyTrainingTimeTv;
    @BindView(R.id.tv_apply_training)
    TextView tvApplyTraining;
    @BindView(R.id.iv_round)
    ImageView ivRound;
    @BindView(R.id.commit_list_success)
    TextView commitListSuccess;
    @BindView(R.id.tv_training_des)
    TextView tvTrainingDes;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_training_apply_detail);
        ButterKnife.bind(this);
        initTitle();
        Intent intent = getIntent();
        if (intent != null) {
            Bundle bundle = intent.getExtras();
            String number = bundle.getString("number");
            requestTrainingDetail(number);
        }

    }

    private void requestTrainingDetail(String number) {
        String token = UserManager.getInstance().getToken();
        String userId = UserManager.getInstance().getUserId();
        String url = APPLYTRAININGDETAIL + "&userId=" + userId + "&token=" + token + "&number=" + number;

        HttpUtil.getInstance().getApiInterface().applyTrainingDetail(url).enqueue(new CallbackImple<TrainingDetailBean>() {
            @Override
            public void onError(Call<TrainingDetailBean> call, Throwable t) {
                ToastUtil.showShortToast(TrainingApplyDetailActivity.this, t.getMessage());
            }

            @Override
            public void onSuccess(Call<TrainingDetailBean> call, TrainingDetailBean response) {
                if (response.getData() != null) {
                    if (response.getStatus() == 200) {
                        TrainingDetailBean.DataBean data = response.getData();
                        String address = data.getAddress();
                        String status = data.getStatus();
                        String title = data.getTitle();
                        String travel = data.getTravel();
                        String stepsCn = data.getStepsCn();
                        String steps = data.getSteps();
                        String departuretime = data.getDeparturetime();
                        TextView mTitle = (TextView) findViewById(R.id.apply_training_title_tv);
                        TextView mCity = (TextView) findViewById(R.id.apply_training_city_tv);
                        TextView mTravel = (TextView) findViewById(R.id.traing_offical_tv);
                        TextView mTime = (TextView) findViewById(R.id.apply_training_time_tv);
                        TextView mSteps = (TextView) findViewById(R.id.commit_list_success);
                        TextView mStepsCn = (TextView) findViewById(R.id.tv_training_des);

                        mTime.setText(departuretime);
                        mTitle.setText(title);
                        mCity.setText(address);
                        mSteps.setText(steps);
                        mStepsCn.setText(stepsCn);
                        mTravel.setText(travel);

                    } else {
                        ToastUtil.showShortToast(TrainingApplyDetailActivity.this, response.getMsg());
                    }

                } else {
                    ToastUtil.showShortToast(TrainingApplyDetailActivity.this, "数据为空");
                }
            }
        });


    }


    private void initTitle() {
        ImageView mBackImg = (ImageView) findViewById(R.id.iv_back);
        mBackImg.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                this.finish();
                break;
        }
    }
}
